package com.homedo.common.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;


@Service
public class RedisQueueService<T> {

    @Autowired
    protected RedisTemplate redisTemplate;

    /**
     * 压栈
     *
     * @param key
     * @param value
     * @return
     */
    public Long leftPush(String key, T value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    /**
     * 队列压栈
     *
     * @param key
     * @param values
     * @return
     */
    public Long leftPushAll(String key, Collection<T> values) {
        return redisTemplate.opsForList().leftPushAll(key, values);
    }

    /**
     * 队列压栈
     *
     * @param key
     * @param values
     * @return
     */
    public Long rightPushAll(String key, Collection<T> values) {
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    /**
     * 出栈出队
     *
     * @param key
     * @return
     */
    public T pop(String key) {
        return (T) redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 入队
     *
     * @param key
     * @param value
     * @return
     */
    public Long rightPush(String key, T value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }


    /**
     * 范围检索
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<T> range(String key, int start, int end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 移除
     *
     * @param key
     * @param i
     * @param value
     */
    public void remove(String key, long i, String value) {
        redisTemplate.opsForList().remove(key, i, value);
    }

    /**
     * 检索
     *
     * @param key
     * @param index
     * @return
     */
    public T index(String key, long index) {
        return (T) redisTemplate.opsForList().index(key, index);
    }

    /**
     * 设值
     *
     * @param key
     * @param index
     * @param value
     */
    public void set(String key, long index, T value) {
        redisTemplate.opsForList().set(key, index, value);
    }

    /**
     * 裁剪
     *
     * @param key
     * @param start
     * @param end
     */
    public void trim(String key, long start, int end) {
        redisTemplate.opsForList().trim(key, start, end);
    }

    /**
     * 队列大小
     *
     * @param key
     */
    public Long size(String key) {
        return redisTemplate.opsForList().size(key);
    }
}
